package gae.testjig.dao;

import gae.testjig.dto.ContactGroup;
import gae.testjig.dto.Organization;

import java.util.LinkedList;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;


public class DaoOrganization {
	
	static public Organization create(PersistenceManager pm, String name) {
		Organization org = new Organization();
		org.setName(name);
		org.setContactGroups(new LinkedList<ContactGroup>()); // empty list
		pm.makePersistent(org);
		return org;
	}
	
	static public Organization findByOrganizationKey(PersistenceManager pm, Key key) throws DaoException {
		Organization org = pm.getObjectById(Organization.class, key);
		if ( org == null ) {
			throw new DaoNotFoundException();
		}
		return org;
	}

	public static Organization delete(PersistenceManager pm, Key organizationKey) throws DaoException {
		Organization org = pm.getObjectById(Organization.class, organizationKey);
		if ( org == null ) {
			throw new DaoNotFoundException();
		}
		Organization org2 = pm.detachCopy(org);
		pm.deletePersistent(org);
		return org2;
	}
	
}
